package com.lml.poi;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.joda.time.DateTime;
import org.junit.jupiter.api.Test;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author luomingliang
 * @date 2021/9/27 23:03
 */
public class ExcelWriteTest {

    String PATH = "D:\\IDEA_workspace2\\poiAndEasyExcel\\src\\main\\resources\\excelDirectory\\poi\\";

    /**
     * 03与07版excel的区别：
     *      1.使用对象不同，03使用HSSFWorkbook，07使用XSSFWorkbook
     *      2.03的最大只能导入65536行，07版可导入1048576行
     *      3.文件后缀名不同，03的后缀为xls，07的后缀为xlsx
     */

    //03版Excel HSSFWorkbook
    @Test
    public void testWrite03() throws IOException {
        //1.创建一个工作簿
        Workbook workbook = new HSSFWorkbook();
        //2.创建一个工作表
        Sheet sheet = workbook.createSheet("狂神观众统计表");
        //3.创建一个行 0代表第一行
        Row row1 = sheet.createRow(0);
        //4.创建一个单元格 (1,1)
        Cell cell11 = row1.createCell(0);
        //5.将表格中填充数据
        cell11.setCellValue("今日新增观众");

        //(1,2)
        Cell cell12 = row1.createCell(1);
        cell12.setCellValue(666);

        //第二行
        Row row2 = sheet.createRow(1);
        //(2,1)
        Cell cell21 = row2.createCell(0);
        cell21.setCellValue("统计时间");
        //(2,2)
        Cell cell22 = row2.createCell(1);
        String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
        cell22.setCellValue(time);

        //生成一张表（IO流） 注：03版本使用xls结尾
        FileOutputStream fileOutputStream = new FileOutputStream(PATH + "狂神观众统计表03.xls");
        //输出
        workbook.write(fileOutputStream);
        //关闭流
        fileOutputStream.close();

        System.out.println("[狂神观众统计表03.xls]生成完毕...");
    }

    //07版Excel XSSFWorkbook
    @Test
    public void testWrite07() throws IOException {
        //1.创建一个工作簿
        Workbook workbook = new XSSFWorkbook();
        //2.创建一个工作表
        Sheet sheet = workbook.createSheet("狂神观众统计表");
        //3.创建一个行 0代表第一行
        Row row1 = sheet.createRow(0);
        //4.创建一个单元格 (1,1)
        Cell cell11 = row1.createCell(0);
        //5.将表格中填充数据
        cell11.setCellValue("今日新增观众");

        //(1,2)
        Cell cell12 = row1.createCell(1);
        cell12.setCellValue(666);

        //第二行
        Row row2 = sheet.createRow(1);
        //(2,1)
        Cell cell21 = row2.createCell(0);
        cell21.setCellValue("统计时间");
        //(2,2)
        Cell cell22 = row2.createCell(1);
        String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
        cell22.setCellValue(time);

        //生成一张表（IO流） 注：07版本使用xlsx结尾
        FileOutputStream fileOutputStream = new FileOutputStream(PATH + "狂神观众统计表07.xlsx");
        //输出
        workbook.write(fileOutputStream);
        //关闭流
        fileOutputStream.close();

        System.out.println("[狂神观众统计表07.xlsx]生成完毕...");
    }

    //大量数据 03版
    @Test
    public void testWrite03BigDate() throws IOException {
        //开始时间
        long begin = System.currentTimeMillis();

        //创建工作簿
        Workbook workbook = new HSSFWorkbook();
        //创建表
        Sheet sheet = workbook.createSheet();
        //写入数据
        for (int rowNum = 0; rowNum < 65536; rowNum++) {
            Row row = sheet.createRow(rowNum);
            for (int cellNum = 0; cellNum < 10; cellNum++) {
                Cell cell = row.createCell(cellNum);
                cell.setCellValue(cellNum);
            }
        }
        System.out.println("over...");
        FileOutputStream outputStream = new FileOutputStream(PATH + "testWrite03BigData.xls");
        workbook.write(outputStream);
        outputStream.close();
        //结束时间
        long end = System.currentTimeMillis();
        System.out.println("用时(秒)：" + (double)(end - begin)/1000);
    }

    //大数据 07版
    @Test
    public void testWrite07BigData() throws IOException {
        //开始时间
        long begin = System.currentTimeMillis();

        //创建工作簿
        Workbook workbook = new XSSFWorkbook();
        //创建表
        Sheet sheet = workbook.createSheet();
        //写入数据
        for (int rowNum = 0; rowNum < 100_000; rowNum++) {
            Row row = sheet.createRow(rowNum);
            for (int cellNum = 0; cellNum < 10; cellNum++) {
                Cell cell = row.createCell(cellNum);
                cell.setCellValue(cellNum);
            }
        }
        System.out.println("over...");
        FileOutputStream outputStream = new FileOutputStream(PATH + "testWrite03BigData.xlsx");
        workbook.write(outputStream);
        outputStream.close();
        //结束时间
        long end = System.currentTimeMillis();
        System.out.println("用时(秒)：" + (double)(end - begin)/1000);
    }

    //大数据 07版 加强版
    @Test
    public void testWrite07BigDataS() throws IOException {
        //开始时间
        long begin = System.currentTimeMillis();

        //创建工作簿
        Workbook workbook = new SXSSFWorkbook();
        //创建表
        Sheet sheet = workbook.createSheet();
        //写入数据
        for (int rowNum = 0; rowNum < 100_000; rowNum++) {
            Row row = sheet.createRow(rowNum);
            for (int cellNum = 0; cellNum < 10; cellNum++) {
                Cell cell = row.createCell(cellNum);
                cell.setCellValue(cellNum);
            }
        }
        System.out.println("over...");
        FileOutputStream outputStream = new FileOutputStream(PATH + "testWrite03BigDataS.xlsx");
        workbook.write(outputStream);
        outputStream.close();
        //清除临时文件
        ((SXSSFWorkbook) workbook).dispose();
        //结束时间
        long end = System.currentTimeMillis();
        System.out.println("用时(秒)：" + (double)(end - begin)/1000);
    }
}
